/*
 *  Copyright (C) 2009  Peter Kist & Jan Ripke
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "MainMenu.h"

#include <core/Logger.h>
#include <gfx/ogre/Types.h>
#include <gfx/ogre/gui/MenuManager.h>
#include <mediator/InterMediator.h>

#include <CEGUIWindowManager.h>
#include <CEGUIEventArgs.h>
#include <elements/CEGUIPushButton.h>

namespace galaxy {
    namespace gfx {
        namespace ogre3d {
            namespace menu {

                MainMenuButton::Name MainMenuButton::c_QuitButton         = "Exit";
                MainMenuButton::Name MainMenuButton::c_SettingsButton     = "Settings";
                MainMenuButton::Name MainMenuButton::c_CreditsButton      = "Credits";
                MainMenuButton::Name MainMenuButton::c_SinglePlayerButton = "SinglePlayer";
                MainMenuButton::Name MainMenuButton::c_MultiPlayerButton  = "MultiPlayer";

                bool MainMenu::load () {
                    LOG_TRACE ("loading menu MainMenu");
                    m_Window = 
                        CEGUI::WindowManager::getSingleton().loadWindowLayout("mainmenu.layout", "", "GUI");
                    return false;
                }

                bool MainMenu::setup () {
                    LOG_TRACE ("setup of menu MainMenu");
                    CEGUI::WindowManager& winMngr = CEGUI::WindowManager::getSingleton();

                    CEGUI::PushButton * quit = 
                        (CEGUI::PushButton *) winMngr.getWindow(MainMenuButton::c_QuitButton);
                    quit->subscribeEvent (CEGUI::PushButton::EventClicked,
                        CEGUI::Event::Subscriber (&MainMenu::Quit_OnClick, this));

                    CEGUI::PushButton * credits = 
                        (CEGUI::PushButton *) winMngr.getWindow(MainMenuButton::c_CreditsButton);
                    credits->subscribeEvent (CEGUI::PushButton::EventClicked,
                        CEGUI::Event::Subscriber (&MainMenu::Credits_OnClick, this));

                    CEGUI::PushButton * settings = 
                        (CEGUI::PushButton *) winMngr.getWindow(MainMenuButton::c_SettingsButton);
                    settings->subscribeEvent (CEGUI::PushButton::EventClicked,
                        CEGUI::Event::Subscriber (&MainMenu::Settings_OnClick, this));

                    CEGUI::PushButton * single = 
                        (CEGUI::PushButton *) winMngr.getWindow(MainMenuButton::c_SinglePlayerButton);
                    single->subscribeEvent (CEGUI::PushButton::EventClicked,
                        CEGUI::Event::Subscriber (&MainMenu::SinglePlayer_OnClick, this));

                    CEGUI::System::getSingleton().setDefaultMouseCursor((CEGUI::utf8*)"TaharezLook",
                        (CEGUI::utf8*)"MouseArrow");
                    const CEGUI::Image * mouseImage = CEGUI::System::getSingleton().getDefaultMouseCursor();
                    CEGUI::MouseCursor::getSingleton().setImage(mouseImage);

                    return true;
                }

                bool MainMenu::unload () {
                    if (m_Window) {
                        LOG_TRACE ("unloading menu MainMenu");
                        CEGUI::WindowManager::getSingleton().destroyWindow(m_Window);
                        m_Window = 0L;
                    }
                    return true;
                }

                /** Handle click on 'exit' button. Notifies logic to change state to ExitState. */
                bool MainMenu::Quit_OnClick(const CEGUI::EventArgs &args) {
                    return changeState (logic::State::c_Exit);
                }

                /** Handle click on 'credits' button. Notifies logic to change state to CreditsState. */
                bool MainMenu::Credits_OnClick(const CEGUI::EventArgs &args) {
                    return changeState (logic::State::c_Credits);
                }

                /** Handle click on 'settings' button. Notifies logic to change state to SettingsState. */
                bool MainMenu::Settings_OnClick(const CEGUI::EventArgs &args) {
                    return changeState (logic::State::c_Settings);
                }

                /** Handle click on 'single-player' button. Notifies logic to change state to GameState. */
                bool MainMenu::SinglePlayer_OnClick(const CEGUI::EventArgs &args) {
                    return changeState (logic::State::c_SinglePlayer);
                }

            } // namespace menu
        } // namespace ogre3d
    } // namespace gfx
} // namespace galaxy
